define(function (require, exports, module) {
    var angular = require('angular');
    var config = require('/public/js/myservice/config');
    require("pagination");
    require("angular-ui");
    require('/public/js/myservice/contract/contract-common');
    var util = require("util");
    var api_prefix = G_CONFIG["api_prefix"]; //api前缀

    angular.module('IncomeInfor',[])
        .service('incomeService', ['$rootScope', function ($rootScope) {
            var service = {
                onCarryoverShow: function ($data, $dialog, $dialogScope) {
                    //$dialogScope.createby_9 = '999';
                    //$dialogScope.createby_0 = '000';
                    $dialogScope.getCarryoverInfo = function () {
                        var url = api_prefix + "/income/get-carryover-info";

                        util.myAjax({
                            url: url,
                            type: "POST",
                            data: {},
                            success: function (json) {
                                if (json.retCode === 0) {
                                    $dialogScope.createby_0 = json.data.createby_0;
                                    $dialogScope.createby_1 = json.data.createby_1;
                                    $dialogScope.createby_9 = json.data.createby_9;
                                    $dialogScope.createon_0 = json.data.createon_0;
                                    $dialogScope.createon_1 = json.data.createon_1;
                                    $dialogScope.createon_9 = json.data.createon_9;
                                    $dialogScope.$applyAsync;
                                } else {
                                    util.alert("获取结转信息失败：" + json.errMsg);
                                }
                            }
                        });
                    };
                    
                    $dialogScope.docarryvover = function (stype) {
                        if([0,1,9].indexOf(stype) == -1){
                            util.alert('请选择结转方式!');
                            return;
                        }
                        var url = api_prefix + "/income/do-carrover";

                        util.myAjax({
                            url: url,
                            type: "POST",
                            data: {type:stype},
                            success: function (json) {
                                if (json.retCode === 0) {
                                    util.alert("结转成功!");
                                    $dialogScope.getCarryoverInfo();
                                } else {
                                    util.alert("执行结转失败：" + json.errMsg);
                                }
                            }
                        });
                    };

                    $dialogScope.getCarryoverInfo();
                }
            };
            return service;
        }])

        .controller('incomeListCtrl',['$scope','$location', '$routeParams','incomeService',function($scope,$location, $routeParams,incomeService){
            $scope.incomeService = incomeService;
            var incometype = $location.url().replace('/','');
            $scope.incomeType = incometype;
            $scope.keyword = '';

            $scope.filterIncomeResource = [
                {name: '全部', selected: true, value: ''},
                {name: '本期新增', value: '0'},
                {name: '上期结转', value: '1'}
            ];

            $scope.filterConfirmType = [
                {name: '全部', selected: true, value: ''},
                {name: '一次性确认', value: '0'},
                {name: '按租期确认', value: '1'}
            ];
            
            $scope.changeConfirmType = function () {
                $scope.query.execute();
            };
            $scope.changeIncomeResource = function () {
                $scope.query.execute();
            };

            //查询条件
            $scope.getQueryParams = function (page) {
                var parms = {keyword: $scope.query.keyword};
                parms.pageIndex = page || 1;
                parms.pageSize = config.pager().pageSize;

                //收集过滤条件
                parms.incometype = $scope.incomeType;
                //确认方式
                filterConfirmType = $.grep($scope.filterConfirmType, function (o,n) {
                    return o.selected == true;
                });
                if(filterConfirmType.length){
                    parms.confirmtype = filterConfirmType[0].value;
                };
                //收入来源:
                filterIncomeResource = $.grep($scope.filterIncomeResource, function (o, n) {
                    return o.selected == true;
                });
                if (filterIncomeResource.length) {
                    parms.incomeresource = filterIncomeResource[0].value;
                };
                return parms;
            };

            $scope.query = {
                execute: function(page){
                    var parms = $scope.getQueryParams(page);
                    var put_data = parms;

                    var url = api_prefix + "/income/get-income-list";

                    util.myAjax({
                        url: url,
                        type: "POST",
                        data: put_data,
                        success: function (json) {
                            if (json.retCode === 0) {
                                $scope.incomes = json.data.items;
                                $scope.$applyAsync();

                                $scope.startPageIndex = (parms.pageIndex - 1) * 10;
                                $('#pagination').pagination(parseInt(json.data.total), {
                                    current_page: parms.pageIndex - 1,
                                    num_edge_entries: 1, //边缘页数
                                    num_display_entries: 4, //主体页数
                                    callback: function (page_index, jq) {
                                        $scope.startPageIndex = (page_index - 1) * 10;
                                        $scope.query.execute(page_index);
                                    },
                                    items_per_page: config.pager().pageSize //每页显示1项
                                });

                            } else {
                                util.alert("获取合同列表失败：" + json.errMsg);
                            }
                        }
                    });
                }
            };

            //导出
            $scope.export = function () {
                util.confirm('确定要导出数据吗？', {
                    ok: function () {
                        var parms = $scope.getQueryParams(1);
                        var url = api_prefix + '/income/export-income';
                        util.download(url, parms);
                    }
                });
            };

            $scope.query.execute();
        }])
})